package com.ivr.cm.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.ivr.cm.dao.ILimitBusinessDao;
import com.ivr.cm.util.BaseUtil;
import com.ivr.cm.vo.LimitBusinessVo;

/**
 * 业务总量配置信息实现
 * @author liugeng
 * @date 2015-12-15
 * @version 1.0
 */
public class LimitBusinessDaoImpl extends JdbcDaoSupport implements ILimitBusinessDao {
	private static Logger logger = Logger.getLogger(LimitBusinessDaoImpl.class);
	
	/**根据当前日期获得业务总量数据列表*/
	@SuppressWarnings("unchecked")
	@Override
	public List<LimitBusinessVo> getLimitBusinessList() {
		String sql = "select lower_id, province, day_limit, month_limit from tb_ivr_limit_business where status = 1";
		logger.info(String.format("根据当前日期获得业务总量数据列表[%s]", sql));
		
		List<LimitBusinessVo> limitBusinessVoList = this.getJdbcTemplate().query(sql, rowMapper);
		if(limitBusinessVoList != null && limitBusinessVoList.size() > 0){
			return limitBusinessVoList;
		}
		return null;
	}
	
	private ParameterizedRowMapper<LimitBusinessVo> rowMapper = new ParameterizedRowMapper<LimitBusinessVo>() {
		@Override
		public LimitBusinessVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			LimitBusinessVo limitBusinessVo = new LimitBusinessVo();
			limitBusinessVo.setLower_id(rs.getInt("lower_id"));
			limitBusinessVo.setProvince(rs.getString("province"));
			limitBusinessVo.setDay_limit(rs.getInt("day_limit"));
			limitBusinessVo.setMonth_limit(rs.getInt("month_limit"));
			return limitBusinessVo;
		}
	};
	/**根据业务长号码获得对应的渠道信息ID*/
	@SuppressWarnings("unchecked")
	@Override
	public int getLowerIdBySpNumber(String sp_number) {
		String sql = "select lower_id from tb_ivr_config_router where sp_number = ?";
		Object[] params = new Object[]{sp_number};
		logger.info(String.format("根据当前日期获得业务总量数据列表[%s]", BaseUtil.logSQL(sql, params)));
		
		List<Integer> lowerIdList = this.getJdbcTemplate().query(sql, params, tempMapper);
		if(lowerIdList != null){
			return lowerIdList.get(0);
		}
		return 0;
	}
	private ParameterizedRowMapper<Integer> tempMapper = new ParameterizedRowMapper<Integer>() {
		@Override
		public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
			return rs.getInt("lower_id");
		}
	};
	/**根据渠道ID和省份获得渠道在当前省份当日已经拨打金额*/
	@Override
	public int getLowerUsedDay(int lower_id, String province, String day_time) {
		String sql = "select floor(ifnull(sum(fee_number),0)/100) fee_number from tb_ivr_user_bill where lower_id = ? and province = ? and date_format(str_to_date(time_end,'%Y-%m-%d'),'%Y-%m-%d') = ? and isfilter = 0";
		Object[] param = new Object[]{lower_id, province, day_time};
		logger.info(String.format("根据渠道ID和省份获得渠道在当前省份当日已经拨打金额[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().queryForInt(sql, param);
		return count;
	}
	/**根据渠道ID和省份获得渠道在当前省份当月已经拨打金额*/
	@Override
	public int getLowerUsedMonth(int lower_id, String province, String month_time) {
		String sql = "select floor(ifnull(sum(fee_number),0)/100) fee_number from tb_ivr_user_bill where lower_id = ? and province = ? and date_format(str_to_date(time_end,'%Y-%m'),'%Y-%m') = ? and isfilter = 0";
		Object[] param = new Object[]{lower_id, province, month_time};
		logger.info(String.format("根据渠道ID和省份获得渠道在当前省份当月已经拨打金额[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().queryForInt(sql, param);
		return count;
	}
}